ALTER PROCEDURE [dbo].[CPP_User_LuckyWheelExchange]
  @dwUserID INT,    -- 用户ID
  @szResult NVARCHAR(255) OUTPUT  -- 输出信息
AS
BEGIN

  SET NOCOUNT ON
  
  DECLARE @szClientIP NVARCHAR(15) = ''
  DECLARE @Channel INT = 0
  SELECT @Channel = Channel FROM RYAccountsDB.dbo.AccountsInfo WHERE AccountsType = 0 AND UserID = @dwUserID
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'玩家[' + CAST(@dwUserID AS NVARCHAR) + ']不存在'
    RETURN 1
  END

  -- 渠道号判断
  DECLARE @FREE_COUNT INT = 0 -- 每日免费次数
  DECLARE @COST_COIN INT = 0  -- 单次消耗金币
  SELECT @FREE_COUNT = LuckyWheelFreeCount, @COST_COIN = ABS(LuckyWheelCostCoin) FROM RYPlatformDB.dbo.GameChannel WHERE ID = @Channel
  IF @@ROWCOUNT=0 BEGIN
    SET @szResult = N'渠道[' + CAST(@Channel AS NVARCHAR) + ']不存在'
    RETURN 2
  END
  
  DECLARE @FreeToday INT = 0  -- 今日免费使用次数
  SELECT @FreeToday = COUNT(ID) FROM RYRecordDB.dbo.RecordLuckyWheelExchange WHERE UserID = @dwUserID AND DATEDIFF(dd, CreateTime, GETDATE()) = 0 AND CostCoin = 0

  DECLARE @DateID INT = CAST(CAST(GETDATE() AS FLOAT) AS INT)
  DECLARE @CostCoin INT = 0
  -- 是否使用金币摇, 优先使用免费次数
  IF @FreeToday>=@FREE_COUNT BEGIN

    SET @CostCoin = @COST_COIN

    -- 判断金币够不够
    DECLARE @Score BIGINT = 0 -- 身上金币
    DECLARE @Insure BIGINT =0 -- 保险箱金币
    SELECT @Score = Score, @Insure = InsureScore FROM RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @dwUserID
    IF @Score<@COST_COIN BEGIN
      SET @szResult = N'您身上的金币数不足'
      RETURN 3
    END

    -- 扣除身上金币
    UPDATE RYTreasureDB.dbo.GameScoreInfo SET Score = Score - @COST_COIN WHERE UserID = @dwUserID

    -- 记录金币帐变
    INSERT INTO [RYTreasureDB].[dbo].[RecordPresentInfo] (
      [UserID], [PreScore], [PreInsureScore], [PresentScore], [TypeID], [IPAddress]
    ) VALUES (
      @dwUserID, @Score, @Insure, @COST_COIN, 15, @szClientIP
    )

    -- 用户转盘金币消耗日统计
    UPDATE [RYTreasureDB].[dbo].[StreamPresentInfo] SET
      [PresentCount]=[PresentCount]+1, [PresentScore]=[PresentScore]+@COST_COIN, [LastDate]=GETDATE()
    WHERE ([DateID]=@DateID) AND ([UserID]=@dwUserID) AND ([TypeID]=15);

    IF @@ROWCOUNT=0 BEGIN
      INSERT INTO [RYTreasureDB].[dbo].[StreamPresentInfo] (
        [DateID], [UserID], [TypeID], [PresentCount], [PresentScore]
      ) VALUES (
        @DateID, @dwUserID, 15, 1, @COST_COIN
      )
    END

  END

  -- 产生随机奖品
  ------------------------------------------------------------------------------------
  -- 中奖率转换成字符串a-z
  DECLARE @RateCode NVARCHAR(255) = ''
  SELECT
    @RateCode = @RateCode + REPLICATE (CHAR (96 + ROW_NUMBER), t.Rate)
  FROM
    (
      SELECT
        a.Rate, ROW_NUMBER () OVER (ORDER BY a.ID) AS ROW_NUMBER
      FROM
        RYPlatformDB.dbo.LuckyWheelItem AS a
      WHERE a.Nullity = 0 AND a.Channel = @Channel
    ) t

  -- 产生[1-100]随机数
  DECLARE @RandNum INT = CEILING(RAND() * LEN(@RateCode))
  DECLARE @RandRow INT = ASCII(SUBSTRING (@RateCode, @RandNum, 1)) - 96

  -- ROW_NUMBER转换成奖品ID
  DECLARE @GoodsID INT = 0  -- 奖品ID
  DECLARE @GoodsType INT = 0  -- 奖品类型
  DECLARE @GoodsReward INT = 0  -- 奖品面值
  DECLARE @GoodsKindID INT = 0  -- 奖品对应游戏
  DECLARE @GoodsTitle NVARCHAR(255) = ''  -- 奖品标题
  SELECT @GoodsID = t.ID, @GoodsType = Type, @GoodsReward = Reward, @GoodsKindID = KindID, @GoodsTitle = Title FROM(
    SELECT
      a.ID, a.Type, a.Reward, a.KindID, a.Title, ROW_NUMBER () OVER (ORDER BY a.ID) AS ROW_NUMBER
    FROM
      RYPlatformDB.dbo.LuckyWheelItem AS a
    WHERE a.Nullity = 0 AND a.Channel = @Channel
  ) t
  WHERE t.ROW_NUMBER = @RandRow
  ------------------------------------------------------------------------------------

  -- 写转盘记录
  INSERT INTO [RYRecordDB].[dbo].[RecordLuckyWheelExchange] ([UserID], [Point], [CostCoin]) VALUES (@dwUserID, @GoodsID, @CostCoin)

  -- 判断奖品类型&加减对应字段&写帐变记录    奖品类型,0=空类型;1=奖励金币;2=奖励房卡;
  IF @GoodsType=1 BEGIN -- 奖励金币

    SELECT @Score = Score, @Insure = InsureScore FROM RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @dwUserID
    -- 赠送奖品金币
    UPDATE RYTreasureDB.dbo.GameScoreInfo SET Score = Score + @GoodsReward WHERE UserID = @dwUserID

    -- 记录金币帐变
    INSERT INTO [RYTreasureDB].[dbo].[RecordPresentInfo] (
      [UserID], [PreScore], [PreInsureScore], [PresentScore], [TypeID], [IPAddress]
    ) VALUES (
      @dwUserID, @Score, @Insure, @GoodsReward, 14, @szClientIP
    )

    -- 用户转盘金币奖励日统计
    UPDATE [RYTreasureDB].[dbo].[StreamPresentInfo] SET
      [PresentCount]=[PresentCount]+1, [PresentScore]=[PresentScore]+@GoodsReward, [LastDate]=GETDATE()
    WHERE ([DateID]=@DateID) AND ([UserID]=@dwUserID) AND ([TypeID]=14);

    IF @@ROWCOUNT=0 BEGIN
      INSERT INTO [RYTreasureDB].[dbo].[StreamPresentInfo] (
        [DateID], [UserID], [TypeID], [PresentCount], [PresentScore]
      ) VALUES (
        @DateID, @dwUserID, 14, 1, @GoodsReward
      )
    END

  END ELSE IF @GoodsType=2 BEGIN  -- 奖励房卡

    EXEC RYTreasureDB.dbo.GSP_GR_BackCreatePrivateCost20180607 @dwUserID, @GoodsReward, 14, 0, @GoodsKindID;

  END

  SELECT @Score = Score, @Insure = InsureScore FROM RYTreasureDB.dbo.GameScoreInfo WHERE UserID = @dwUserID

  SELECT @dwUserID AS UserID, @Score AS Score, @Insure AS Insure, @GoodsID AS GoodsID, @GoodsTitle AS GoodsTitle
  
  SET @szResult = 'SUCCESS'
  RETURN 0
END